<template>
  <div class="class-create-root">
    <!-- @closed 窗口关闭时结束时的回调   -->
    <el-drawer v-model="classManagerStore.attendanceManagerDialogVisible" :title="attendanceSetTitle" size="1100"
               @closed="toggleDialog"
               style="text-align: center;font-weight:bold;"
    >
      <div class="select-date">
        <el-date-picker
            v-model="attendanceDate"
            type="date"
            placeholder="请选择日期"
            value-format="YYYYMMDD"
            :size="size"
            @change="dateChange"
        />
        <el-button type="primary" style="margin-left: 50px" @click="attendanceInit">设置考勤全部正常</el-button>
      </div>

      <el-table :data="tableData" style="width: 100%;">
        <el-table-column fixed prop="student_info.name" label="学员姓名" width="80" />
        <el-table-column fixed="right" label="考勤情况" min-width="90">
          <template #default="{ row }">
            <el-select
                v-model="row.selectedAttendanceValue"
                placeholder="考勤状态"
                size="large"
                style="width: 120px"

            >
              <el-option v-for = "(itemValue, itemKey) in ATTENDANCE_STATUS" :key="itemKey" :label="itemValue" :value="itemKey" />

            </el-select>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="异常类型" min-width="120">
          <template #default="{ row }">
            <el-select
                v-model="row.selectedAbnormalType"
                placeholder="选择异常类型"
                size="large"
                style="width: 160px"
            >
              <el-option v-for="(value, key) in sysDictStore.ABNORMAL_TYPE" key={{key}} :label="value" :value="parseInt(key)" />
            </el-select>
         </template>
        </el-table-column>
        <el-table-column fixed="right" label="备注" min-width="350">
          <template #default="{ row }">
            <el-input v-model="row.userInputValue" style="width: 490px" placeholder="请输入异常原因，无异常不输入" />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="50">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="attendanceUpdate(scope.row)">确定</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup>
import {onMounted, ref, watch} from 'vue'
import { useClassManagerStore } from "@/store/store-class-manager-pinia";
import {
  getAttendanceByDateAndClassAPI,
  setAttendanceByDateAPI,
  updateAttendanceByIdAPI
} from "@/network/attendance/attendance";
import {getToday} from "@/utils/util-date";
import {ElMessage} from "element-plus";
import {ATTENDANCE_STATUS} from "@/dict/dict-attendance";
import {useSysDictStore} from "@/store/store-sys-dict-pinia";
import EventBus from "@/plugins/event-bus";
import {SYS_DICT} from "@/dict/dict-sys";


const classManagerStore = useClassManagerStore();
const sysDictStore = useSysDictStore();
sysDictStore.getSysDict([SYS_DICT.ABNORMAL_TYPE]);


let tableData = ref([])
let attendanceSetTitle=ref("")
let classId=ref()

const toggleDialog = () => {
  // dialogFormVisible.value = true;
  tableData.value = [];
  classManagerStore.toggleAttendanceManagerDialogVisible();
};

// 日期控件的大小
const size = "default";
// 日期绑定的变量，获取今天的日期为默认值
let attendanceDate=ref(getToday('-'))

const dateChange = (data) =>{
  getAttendanceByDateAndClassAPI(data, classId.value).then(res=>{
    if (res.data.code !== 0) {
      ElMessage.error(res.data.message)
    }
    else {
      tableData.value = res.data.data;
      // 这里需要把tableData里边每一个数据再加上三个字段，考勤情况、考勤类型、详细说明
      for (let i = 0; i < tableData.value.length; i++) {
        // tableData.value[i].selectedAttendanceValue = ATTENDANCE_STATUS[1];
        tableData.value[i].selectedAttendanceValue = tableData.value[i].is_normal===true?"1":"0";
        tableData.value[i].selectedAbnormalType = tableData.value[i].abnormal_type;
        tableData.value[i].userInputValue = tableData.value[i].remark;
      }
    }
  })
  attendanceDate.value = data
}
EventBus.on("update-attendance-info-event",(eventData)=>{
  classId.value=eventData.classId
  attendanceSetTitle.value = eventData.className
  dateChange(getToday(""))
})

const attendanceInit = () =>{
  let data = {
    attend_date: attendanceDate.value,
    class_id: classId.value,
  }
  setAttendanceByDateAPI(data).then(res=>{
    if (res.data.code !== 0) {
      ElMessage.error(res.data.message)
    }
    else {
      ElMessage.success(res.data.message)
      dateChange(attendanceDate.value)
    }
  })
}

const attendanceUpdate = (row) =>{
  let data = {
    id: row.id,
    student_id:row.student_info.id,
    is_normal: parseInt(row.selectedAttendanceValue),
    abnormal_type: row.selectedAbnormalType,
    remark: row.userInputValue,
    attend_date: attendanceDate.value
  }

  updateAttendanceByIdAPI(data).then(res=>{
    if (res.data.code !== 0) {
      ElMessage.error(res.data.message)
    }
    else {
      ElMessage.success(res.data.message)
      dateChange(attendanceDate.value)
    }
})
}

</script>

<style scoped lang="less">
.class-create-root {
  .select-date {
    text-align: left;
    margin: 10px;
  }
}
</style>